package com.wiscamp.ninechapters.problems.infrastructure.mappers.problems;

import com.wiscamp.ninechapters.problems.infrastructure.po.problems.QuestionSolutionPO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;

/**
 * 问题解析仓储对象
 *
 * @author yifeng
 */
@Repository
public interface QuestionSolutionMapper extends JpaRepository<QuestionSolutionPO, Long>, JpaSpecificationExecutor<QuestionSolutionPO> {
    @Query(value = "select e from QuestionSolutionPO e where e.questionId = :questionId order by e.solutionIndex asc")
    Collection<QuestionSolutionPO> findListByQuestion(long questionId);

    /**
     * 通过问题编号删除解析
     *
     * @param questionId
     */
    @Modifying
    @Transactional(transactionManager = "problemsTransactionManager")
    @Query(value = "delete from QuestionSolutionPO e where e.questionId = :questionId")
    void deleteByQuestion(long questionId);
}